package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodDao foodDao;

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Food> page = foodDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //添加
    public void add(Food food) {
        //判断数据库中是否存在相同食物
        Food findFood = foodDao.findFoodByFoodName(food.getFoodName());
        if (findFood == null) {
            //没有新增
            foodDao.add(food);
        } else {
            //有则更新
            foodDao.update(food);
        }
    }

    //根据id删除食物项
    public void deleteById(Integer id) {
        foodDao.deleteById(id);
    }
}
